/**
 * 系统管理器模块
 * 负责系统启动、停止、状态管理
 */

class SystemManager {
    constructor() {
        this.isSystemRunning = false;
        this.init();
    }

    init() {
        this.bindEvents();
        this.loadSystemStatus();
    }

    bindEvents() {
        const startBtn = document.getElementById('startBtn');
        const stopBtn = document.getElementById('stopBtn');

        if (startBtn) {
            startBtn.addEventListener('click', () => this.startSystem());
        }
        
        if (stopBtn) {
            stopBtn.addEventListener('click', () => this.stopSystem());
        }
    }

    async startSystem() {
        try {
            const response = await fetch('/api/system/start', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            });
            const result = await response.json();

            if (result.success) {
                this.updateSystemStatus(true);
                window.logManager?.addLogEntry('info', '系统启动成功');
            } else {
                window.logManager?.addLogEntry('error', `启动失败: ${result.message}`);
            }
        } catch (error) {
            window.logManager?.addLogEntry('error', `启动错误: ${error.message}`);
        }
    }

    async stopSystem() {
        try {
            const response = await fetch('/api/system/stop', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            });
            const result = await response.json();

            if (result.success) {
                this.updateSystemStatus(false);
                window.logManager?.addLogEntry('info', '系统停止成功');
                window.detectionManager?.hideDetectionOverlay();
            } else {
                window.logManager?.addLogEntry('error', `停止失败: ${result.message}`);
            }
        } catch (error) {
            window.logManager?.addLogEntry('error', `停止错误: ${error.message}`);
        }
    }

    updateSystemStatus(running) {
        this.isSystemRunning = running;
        
        const systemStatus = document.getElementById('systemStatus');
        const startBtn = document.getElementById('startBtn');
        const stopBtn = document.getElementById('stopBtn');

        if (systemStatus) {
            if (running) {
                systemStatus.className = 'status-indicator status-running';
                systemStatus.innerHTML = '<div class="status-dot"></div><span>系统运行中</span>';
            } else {
                systemStatus.className = 'status-indicator status-stopped';
                systemStatus.innerHTML = '<div class="status-dot"></div><span>系统停止</span>';
            }
        }

        if (startBtn) startBtn.disabled = running;
        if (stopBtn) stopBtn.disabled = !running;
    }

    async loadSystemStatus() {
        try {
            const response = await fetch('/api/system/status');
            const status = await response.json();
            this.updateSystemStatus(status.running);
        } catch (error) {
            console.error('Failed to load system status:', error);
        }
    }
}

// 导出到全局
window.SystemManager = SystemManager; 